from PyQt4 import QtCore, QtGui
from appalt import Ui_AppAlt
import paramiko

class AlteraApp(QtGui.QDialog):
    def __init__(self, parent, transport):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_AppAlt()
        self.ui.setupUi(self)
        
        self.folder = './log_rastrear/'
        self.oem = 'OEMApplication.txt'
        self.transport = transport
        
        sftp = paramiko.SFTPClient.from_transport(self.transport)
        sftp.get(self.folder+self.oem,'./OEMs/'+self.oem)
        sftp.close()
        
        file = open('./OEMs/'+self.oem)
        lista = file.readlines()
        file.close()
        
        for i in lista:
            self.ui.plainTextEdit.insertPlainText(i)
            
        QtCore.QObject.connect(self.ui.buttonBox,QtCore.SIGNAL("accepted()"),self.salva)
            
    def salva(self):
        fp = open('./OEMs/'+self.oem,'wb')
        fp.writelines(str(self.ui.plainTextEdit.toPlainText()))
        fp.close()

        sftp = paramiko.SFTPClient.from_transport(self.transport)
        sftp.put('./OEMs/'+self.oem,self.folder+self.oem)       
        sftp.close()
